﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using neside3.ext;

namespace editor_txt
{
    public class editor_txt : neside3.ext.IFileEditor
    {
        //这个函数自动反射调用
        //插件只需要继承相应的接口，并拥有一个无参构造函数，即可被发现并初始化
        public editor_txt()
        {

        }

        string projectFolder = null;
        public string[] extName => new[] { ".txt", ".json", ".c", ".s", ".cfg" };

        Dictionary<string, IDocumentInEditor> opendocs = new Dictionary<string, IDocumentInEditor>();
        public IDocumentInEditor OpenFile(ILogger logger, string filename)
        {
            if (this.projectFolder == null)
            {
                logger.Error("editor_txt init error,SetProject first.");
                return null;
            }
            if (opendocs.ContainsKey(filename))
            {
                logger.Error("editor_txt file opend already." + filename);
                return null;
            }
            var doc = new Document(projectFolder, filename);
            return doc;
        }
        public void CloseFile(IDocumentInEditor editor)
        {
            opendocs.Remove(editor.filename);
        }

        public bool SetProject(ILogger logger, string projectfolder)
        {
            if (opendocs.Count != 0)
            {
                logger.Error("你不能在还有文档打开的情况下改变项目路径,Can't change project folder when doc still open.");
                return false;
            }
            this.projectFolder = projectfolder;
            if (System.IO.Directory.Exists(projectfolder) == false)
            {
                logger.Error("editor_txt.SetProject path not Exists:" + projectfolder);
                this.projectFolder = null;
                return false;
            }
            return true;
        }
    }
    public class Document : neside3.ext.IDocumentInEditor
    {
        public Document(string folder, string filename)
        {
            this.projectfolder = folder;
            this.filename = filename;
            this._ui_editor = new editor();
            this._ui_editor.Set(this);
            this._ui_fileinfo = new info();
            this._ui_fileinfo.Set(this);
        }
        public string projectfolder
        {
            get;
            private set;
        }


        public string filename
        {
            get;
            private set;
        }

        private editor _ui_editor;
        public UIElement ui_editor => _ui_editor;

        private info _ui_fileinfo;

        public event Action<IDocumentInEditor> OnChangeInEditor;
        //public event Action<IDocumentInEditor> OnSaveInEditor;
        public void Edit()
        {
            OnChangeInEditor?.Invoke(this);
        }
        public UIElement ui_fileinfo => _ui_fileinfo;
        public void Load()
        {
            _ui_editor.Load();
        }
        public void Save()
        {
            _ui_editor.Save();
        }


    }

}
